﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using CashOrCard.Helper;
using CashOrCard.Models;
using CashOrCard.Repository;

namespace CashOrCard.Controllers.Admin
{
    public class AdminDefaultController : Controller
    {
        private readonly IBaseRepository<User> _userRep;

        public AdminDefaultController(IBaseRepository<User> userRep)
        {
            this._userRep = userRep;
        }

        public ActionResult Index()
        {
            string email = Common.CurrentUserEamil;
            var curUser = _userRep.LoadEntities(x => x.Email == email && x.IsEnable == true).FirstOrDefault();
            if (curUser == null)
            {
                return RedirectToAction("Index", "Home");
            }
            else if (curUser.IsApproved)
            {
                ViewBag.IsAdmin = curUser.Role.RoleName.ToLower() == "admin";
                ViewBag.RoleType = curUser.Role.RoleName.ToLower();
                ViewBag.LoginName = string.Format("{0} {1}", curUser.FirstName, curUser.LastName);
                ViewBag.UserId = curUser.UserId;
                ViewBag.MenuList = Helper.PermissionUtility.GenerateMenuDataList();
                return View();
            }
            else
            {
                return RedirectToAction("ActiveAccount", "Login");
            }
        }
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }
}
